<template>
  <div>
    <!-- 结算单位新增弹窗 -->
    <el-dialog
      width="500px"
      custom-class="customWidth"
      center
      :before-close="closeDialog"
      title="结算单位资料录入"
      :close-on-click-modal="false"
      :visible.sync="addDialogFormVisible"
    >
      <el-form :model="adddataForm" :rules="adddtataRule" ref="adddataForm" label-width="100px">
        <el-form-item label="上级单位" prop="parentMerchantGuid">
          <el-col :span="15">
            <el-select
              v-model="adddataForm.parentMerchantGuid"
              placeholder="请选择"
              style="width:100%"
              size="mini"
              clearable
            >
              <el-option
                v-for="item in parentMerchantGuidoption"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item label="结算单位名称">
          <el-col :span="15">
            <el-input v-model="adddataForm.merchantName" size="mini"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="消费钱包" prop="consumeWalletNo">
          <el-col :span="15">
            <el-select
              v-model="adddataForm.consumeWalletNo"
              placeholder="请选择"
              size="mini"
              clearable
            >
              <el-option
                v-for="item in paylist"
                :key="item.walletNo"
                :label="item.walletName"
                :value="item.walletNo"
              ></el-option>
            </el-select>
          </el-col>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addvisible()">取消</el-button>
        <el-button type="primary" @click="adddataFormSubmit()">确定</el-button>
      </span>
    </el-dialog>
    <!-- 结算单位修改 -->
    <el-dialog
      width="500px"
      custom-class="customWidth"
      center
      :before-close="eidtcloseDialog"
      title="结算单位资料修改"
      :close-on-click-modal="false"
      :visible.sync="eidtDialogFormVisible"
    >
      <el-form :model="eidtdataForm" :rules="eidtdtataRule" ref="eidtdataForm" label-width="100px">
        <el-form-item label="上级单位" prop="parentMerchantGuid">
          <el-col :span="15">
            <el-select
              v-model="eidtdataForm.parentMerchantGuid"
              placeholder="请选择"
              style="width:100%"
              size="mini"
              clearable
            >
              <el-option
                v-for="item in eidtparentMerchantGuidoption"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item label="结算单位名称">
          <el-col :span="15">
            <el-input v-model="eidtdataForm.merchantName" size="mini"></el-input>
          </el-col>
        </el-form-item>

        <el-form-item label="消费钱包" prop="consumeWalletNo">
          <el-col :span="15">
            <el-select
              v-model="eidtdataForm.consumeWalletNo"
              placeholder="请选择"
              size="mini"
              clearable
            >
              <el-option
                v-for="item in paylist"
                :key="item.walletNo"
                :label="item.walletName"
                :value="item.walletNo"
              ></el-option>
            </el-select>
          </el-col>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="eidtvisible()">取消</el-button>
        <el-button type="primary" @click="eiddataFormSubmit()">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      addDialogFormVisible: false,
      eidtDialogFormVisible: false,
      parentMerchantGuidoption: [],
      eidtparentMerchantGuidoption: [],
      eidtparentMerchantGuidlength: "",
      userGuid: "",
      // 钱包列表选项
      paylist: [],
      adddataForm: {
        parentMerchantGuid: "",
        merchantName: "",
        consumeWalletNo: "",
      },
      eidtdataForm: {
        parentMerchantGuid: "",
        merchantName: "",
        merchantGuid: "",
        unitGuid: "",
        consumeWalletNo: "",
      },
      adddtataRule: {
        consumeWalletNo: [
          {
            required: true,
            message: "请选择消费钱包",
            trigger: ["blur"],
          },
        ],
        // parentMerchantGuid: [
        //   {
        //     required: true,
        //     message: "请选择上级单位",
        //     trigger: ["blur", "change"]
        //   }
        // ]
      },
      eidtdtataRule: {
        consumeWalletNo: [
          {
            required: true,
            message: "请选择消费钱包",
            trigger: ["blur"],
          },
        ],
      },
    };
  },
  computed: {
    changeschoolid: {
      get() {
        return this.$store.state.user.changeschoolid;
      },
    },
  },
  created() {
    this.userGuid = sessionStorage.getItem("administrator");
  },
  mounted() {
    this.getPayNumber();
  },
  methods: {
    // 获取钱包
    getPayNumber() {
      this.paylist = [];
      this.$http({
        url: this.$http.adornUrl("/unit/unit-wallet-info-list"),
        method: "post",
        data: this.$http.adornData({
          messageContent: {unitGuid:this.changeschoolid},
          messageType: 0,
        }),
      }).then(({ data }) => {
        if (data.messageCode == 200) {
          data.messageContent.map((item) => {
            if (Number(item.walletNo) != 100) {
              this.paylist.push(item);
            }
          });
          console.log(this.paylist);
        } else {
          this.$message({
            showClose: true,
            message: data.errorMessage,
            type: "error",
          });
        }
      });
    },

    // 递归获取上级单位
    getparentid(Arr) {
      Arr.forEach((ele) => {
        this.parentMerchantGuidoption.push({
          label: ele.label,
          value: ele.name,
        });
        if (ele.children && ele.children.length > 0) {
          this.getparentid(ele.children);
        }
      });
    },
    // 获取上级单位信息
    getarea() {
      this.parentMerchantGuidoption = [];
      this.$http({
        url: this.$http.adornUrl("/terminal/merchant/unit-merchant-list"),
        method: "post",
        data: this.$http.adornData({
          messageContent: {
            unitGuid: this.changeschoolid,
          },
          messageType: 0,
        }),
      }).then(({ data }) => {
        if (data.messageCode == 200) {
          console.log(data);
          // console.log(data.messageContent)
          if (this.parentMerchantGuidoption.length == 0) {
            for (var i = 0; i < data.messageContent.length; i++) {
              this.parentMerchantGuidoption.push({
                label: data.messageContent[i].merchantName,
                value: data.messageContent[i].merchantGuid,
              });
            }
          }
          // this.getparentid(data.messageContent)
          console.log(this.parentMerchantGuidoption);
        } else {
          this.$message.error(data.errorMessage);
        }
      });
    },
    addvisible() {
      this.addDialogFormVisible = false;
      this.$refs["adddataForm"].resetFields();
    },
    // 新增结算单位
    adddataFormSubmit() {
      console.log(this.adddataForm);
      this.$refs["adddataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl("/terminal/merchant/unit-merchant-add"),
            method: "post",
            data: this.$http.adornData({
              messageContent: {
                merchantName: this.adddataForm.merchantName,
                unitGuid: this.changeschoolid,
                parentMerchantGuid: this.adddataForm.parentMerchantGuid,
                consumeWalletNo: this.adddataForm.consumeWalletNo,
              },
              messageType: 0,
            }),
          }).then(({ data }) => {
            if (data.messageCode == 200) {
              this.$message({
                message: "新增成功",
                type: "success",
                duration: 200,
                onClose: () => {
                  this.addDialogFormVisible = false;
                  this.$emit("refreshDataList");
                  this.$refs["adddataForm"].resetFields();
                },
              });
              // console.log(data)
            } else {
              this.$message.error(data.errorMessage);
            }
          });
        }
      });
    },
    init() {
      this.getarea();
      this.addDialogFormVisible = true;
    },
    closeDialog(done) {
      this.$refs["adddataForm"].resetFields();
      this.addDialogFormVisible = false;
      done();
    },
    //编辑
    eidtinit(row) {
      console.log(row);
      this.getarea();
      this.eidtdataForm.merchantName = row.merchantName;
      this.eidtparentMerchantGuidoption = this.parentMerchantGuidoption;
      this.eidtdataForm.merchantGuid = row.merchantGuid;
      this.eidtdataForm.unitGuid = row.unitGuid;
      this.$set(this.eidtdataForm, "consumeWalletNo", row.consumeWalletNo);
      setTimeout(() => {
        console.log(this.eidtparentMerchantGuidoption.length);
        for (var i = 0; i < this.eidtparentMerchantGuidoption.length; i++) {
          if (this.eidtparentMerchantGuidoption[i].value == row.merchantGuid) {
            this.eidtparentMerchantGuidoption.splice(i, 1);
          }
        }
      }, 100);
      this.eidtdataForm.parentMerchantGuid = row.parentMerchantGuid;

      // this.eidtdataForm.consumeWalletNo = row.consumeWalletNo;

      // console.log(this.eidtparentMerchantGuidlength);
      this.eidtDialogFormVisible = true;
    },
    //结算单编辑
    eiddataFormSubmit() {
      console.log(this.eidtdataForm);
      this.$refs["eidtdataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl("/terminal/merchant/unit-merchant-update"),
            method: "post",
            data: this.$http.adornData({
              messageContent: {
                merchantName: this.eidtdataForm.merchantName,
                merchantGuid: this.eidtdataForm.merchantGuid,
                unitGuid: this.eidtdataForm.unitGuid,
                parentMerchantGuid: this.eidtdataForm.parentMerchantGuid,
                consumeWalletNo: this.eidtdataForm.consumeWalletNo,
              },
              messageType: 0,
            }),
          }).then(({ data }) => {
            if (data.messageCode == 200) {
              this.$message({
                message: "编辑成功",
                type: "success",
                duration: 200,
                onClose: () => {
                  this.$emit("refreshDataList");
                  this.eidtDialogFormVisible = false;
                },
              });
            } else {
              this.$message.error(data.errorMessage);
            }
          });
        }
      });
    },
    eidtcloseDialog(done) {
      this.$refs["eidtdataForm"].resetFields();
      this.eidtDialogFormVisible = false;
      done();
    },
    eidtvisible() {
      this.$nextTick(() => {
        this.$refs["eidtdataForm"].resetFields();
      });
      this.eidtDialogFormVisible = false;
    },
  },
};
</script>

<style lang="scss" scoped></style>
